﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLKS.ServiceReferenceQLKS;

namespace QLKS
{
    public partial class FrmNhanVien : Form
    {
        IList<NHANVIEN> lstnhanvien;
        bool insertorupdate = false;
        public static FrmNhanVien frm = null;

        public FrmNhanVien()
        {
            InitializeComponent();
            Initdanhsachnhanvien();
            dateTimeNgaysinh.Format = DateTimePickerFormat.Custom;
            dateTimeNgaysinh.CustomFormat = "MM/dd/yyyy";
            dateTimeNgayVaoLam.Format = DateTimePickerFormat.Custom;
            dateTimeNgayVaoLam.CustomFormat = "MM/dd/yyyy";
            frm = this;
        }
        //Load danh sách nhân viên
        public void Initdanhsachnhanvien()
        {
            NHANVIEN[] outnhanvien;
            var client = new ServiceQLKSClient();
            var error = client.doparserPerson(out outnhanvien, OPTCodeQLKS.OPTCode_NHANVIEN_GETALL, null);
            if (error == ErrorCodeQLKS.CODE_NHANVIEN_GETALL_SUCCESS)
            {
                lstnhanvien = outnhanvien.ToList();
                nHANVIENBindingSource.DataSource = lstnhanvien;
            }
            try
            {
                if (client.State != System.ServiceModel.CommunicationState.Faulted)
                {
                    client.Close();
                }
            }
            catch (Exception ex)
            {
                client.Abort();
            }
        }

        //set thuoc tinh cho doi tuong nhan vien
        public NHANVIEN setproperties()
        {
            NHANVIEN nv = new NHANVIEN();
            nv.MSNV = txtmanv.Text;
            nv.HOTENNV = txthoten.Text;
            if (radNam.Checked == true)
            {
                nv.GIOITINHNV = true;
            }
            else
            {
                nv.GIOITINHNV = false;
            }
            nv.MatKhau = txtmatkhau.Text;
            nv.DIACHINV = txtdiachi.Text;
            nv.DIENTHOAINV = txtdienthoai.Text;
            nv.NGAYSINHNV = dateTimeNgaysinh.Value;
            nv.NGAYVAOLAM = dateTimeNgayVaoLam.Value;
            nv.OPTION = insertorupdate;
            return nv;
        }

        public void funinsertorupdate()
        {
            NHANVIEN nv = new NHANVIEN();
            nv = setproperties();
            var client = new ServiceQLKSClient();
            NHANVIEN[] outnhanvien;
            var error = client.doparserPerson(out outnhanvien, OPTCodeQLKS.OPTCode_NHANVIEN_INSERTUPDATE, nv);
            if (error == ErrorCodeQLKS.CODE_NHANVIEN_INSERTUPDATE_SUCCESS)
            {
                Initdanhsachnhanvien();
            }
            try
            {
                if (client.State != System.ServiceModel.CommunicationState.Faulted)
                {
                    client.Close();
                }
            }
            catch (Exception ex)
            {
                client.Abort();
            }
            insertorupdate = false;
        }
        
        private void btnthem_Click(object sender, EventArgs e)
        {
            txtmanv.Clear();
            txthoten.Clear();
            txtmatkhau.Clear();
            txtdienthoai.Clear();
            txtmatkhau.Focus();
            insertorupdate = true;
            radNam.Checked = true;
            int maxrow = gvnhanvien.RowCount-1;
            string manv = gvnhanvien.Rows[maxrow].Cells["mSNVDataGridViewTextBoxColumn"].Value.ToString();
            string maxmanv =manv.Substring(2).Trim();
            int tmp = Int32.Parse(maxmanv);
            tmp = tmp + 1;
            txtmanv.Text = "NV" + tmp;
        }

        //kiem tra ma nhan vien co hop le ko?
        private void txtmanv_Leave(object sender, EventArgs e)
        {
            if (insertorupdate == true)
            {
                string manv = txtmanv.Text;
                if (manv == "")
                {
                    MessageBox.Show("Mã nhân viên không được trống", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtmanv.Focus();
                    return;
                }
                else
                {
                    foreach (var item in lstnhanvien)
                    {
                        if (item.MSNV == manv)
                        {
                            MessageBox.Show("Mã nhân viên trùng", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            txtmanv.Focus();
                            break;
                        }
                    }
                }
            }
        }

        private void gvnhanvien_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            var item = gvnhanvien.CurrentRow.DataBoundItem as NHANVIEN;
            if (item != null)
            {
                dateTimeNgaysinh.Value = item.NGAYSINHNV;
                dateTimeNgayVaoLam.Value = item.NGAYVAOLAM;
                if (item.GIOITINHNV == true)
                {
                    radNam.Checked = true;
                }
                else
                {
                    radNu.Checked = true;
                }
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            funinsertorupdate();
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            var item = gvnhanvien.CurrentRow.DataBoundItem as NHANVIEN;
            var client = new ServiceQLKSClient();
            NHANVIEN[] outnhanvien;
            var error = client.doparserPerson(out outnhanvien, OPTCodeQLKS.OPTCode_NHANVIEN_DELETE, item);
            if (error == ErrorCodeQLKS.CODE_NHANVIEN_DELETE_SUCCESS)
            {
                Initdanhsachnhanvien();
            }
            try
            {
                if (client.State != System.ServiceModel.CommunicationState.Faulted)
                {
                    client.Close();
                }
            }
            catch (Exception ex)
            {
                client.Abort();
            }
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txttimkiem_TextChanged(object sender, EventArgs e)
        {
            IList<NHANVIEN> lsttmp;
            if (radtimkiemtheomanv.Checked == true)
            {
               
                lsttmp=lstnhanvien.Where(x => x.MSNV.Contains(txttimkiem.Text)).ToList();
                nHANVIENBindingSource.DataSource = lsttmp;
            }
            else
            {
               lsttmp=lstnhanvien.Where(x => x.HOTENNV.Contains(txttimkiem.Text)).ToList();
               nHANVIENBindingSource.DataSource = lsttmp;
            }
           
        }

        private void FrmNhanVien_FormClosed(object sender, FormClosedEventArgs e)
        {
            frm = null;
        }
    }
}
